//
//  AddVidoeViewController.m
//  HongFuShop
//
//  Created by chirui on 2017/12/21.
//  Copyright © 2017年 Shui. All rights reserved.
//

#import "AddVidoeViewController.h"
#import <MediaPlayer/MediaPlayer.h>

@interface AddVidoeViewController ()
{
    
    
    UIImageView * videoImage;  // 视频背景图片
   
}
@property(nonatomic,strong) MPMoviePlayerController * moviePlay;//播放器
@property(nonatomic,strong)UIActivityIndicatorView * activity;//菊花转
@property (nonatomic,strong) UIButton *playButton;
@property(nonatomic,strong)UILabel * videotimeLabel;//视频时间

@end

@implementation AddVidoeViewController


- (void)viewDidLoad {
    [super viewDidLoad];
   
      [self creatNotification];
    [self createUI];
}
-(void)creatNotification{
    
    NSNotificationCenter *notificationCenter=[NSNotificationCenter defaultCenter];
    
    [notificationCenter addObserver:self selector:@selector(foo:) name :MPMoviePlayerPlaybackStateDidChangeNotification object:self.moviePlay];
    
    [notificationCenter addObserver:self selector:@selector(bar:) name :MPMoviePlayerPlaybackDidFinishNotification object:self.moviePlay];
    [notificationCenter addObserver:self selector:@selector(userStop:) name :MPMoviePlayerPlaybackDidFinishReasonUserInfoKey object:self.moviePlay];
    
}
//视频开始播放
-(void)foo:(NSNotification *)center {
    
    [self.activity stopAnimating];
    
}
//视频播放结束
-(void)bar:(NSNotification *)center{
    
    self.moviePlay.view.hidden =YES;
    self.playButton.hidden =NO;
    self.playButton.enabled =YES;
}

-(void)userStop:(NSNotification *)not{
    
    self.moviePlay.view.hidden= YES;
    self.playButton.hidden =NO;
    self.playButton.enabled =YES;
    
    
}

-(void)dealloc{
    
    [[NSNotificationCenter defaultCenter]removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:self.moviePlay];
    [[NSNotificationCenter defaultCenter]removeObserver:self name:MPMoviePlayerPlaybackStateDidChangeNotification object:self.moviePlay];
    
    
}

-(void)backButton {
    [self.moviePlay stop];
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)createUI{
    
    self.view.backgroundColor = [UIColor whiteColor];
    // 导航栏
    TitleView(@"详情");
    
    self.navigationItem.leftBarButtonItem = [UIBarButtonItem itemWithIcon:@"icon_public_return" target:self action:@selector(backButton)];
    
    UIView * videoView =[[UIView alloc]initWithFrame:CGRectMake(0, [[UIApplication sharedApplication] statusBarFrame].size.height + 44, WIDTH, 170)];
    videoView.backgroundColor =[UIColor clearColor];
    
    videoImage =[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, 170)];
    videoImage.image =[UIImage imageNamed:@"img_home_banner2"];
    
    [videoImage sd_setImageWithURL:ImageUrl(self.dataDic[@"src"])];
    videoImage.userInteractionEnabled =YES;
    [videoView addSubview:videoImage];
    
    _playButton =[UIButton buttonWithType:UIButtonTypeCustom];
    
    _playButton.frame = CGRectMake((WIDTH-50)/2-15, 60, 50, 50);
    
    
    [_playButton setBackgroundImage:[UIImage imageNamed:@"icon_pet_video__button"] forState:UIControlStateNormal];
    
    [_playButton addTarget:self action:@selector(playButtonBtnPressed:) forControlEvents:UIControlEventTouchUpInside];
    
    [videoImage addSubview:_playButton];
    
    
    self.moviePlay = [[MPMoviePlayerController alloc]init];
    self.moviePlay.view.frame =CGRectMake(0, 0, CGRectGetWidth(videoImage.frame), CGRectGetHeight(videoImage.frame));
    self.moviePlay.view.hidden =YES;
    self.moviePlay.view.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
    [videoImage addSubview:self.moviePlay.view];
//
//    CGAffineTransform landscapeTransform = CGAffineTransformMakeRotation(M_PI / 2);
//    self.moviePlay.view.transform = landscapeTransform;
//     self.moviePlay.scalingMode = MPMovieScalingModeAspectFill;
    
    _activity =[[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
    [_moviePlay setContentURL:[NSURL URLWithString:self.dataDic[@"url"]]];
    //缓冲动画
    _activity.center = _moviePlay.view.center;
    
    [self.moviePlay.view addSubview:_activity];
    
    [self.view addSubview:videoView];
    
    UILabel * titleLabel  =[[UILabel alloc]initWithFrame:CGRectMake(10, CGRectGetMaxY(videoView.frame)+10, WIDTH - 20, 20)];
    titleLabel.text = self.dataDic[@"title"];
    titleLabel.font = [UIFont systemFontOfSize:15];
    [titleLabel sizeToFit];
    titleLabel.numberOfLines = 0;
    [self.view addSubview:titleLabel];
    
}

-(void)playButtonBtnPressed:(UIButton *)btn {
    
    self.moviePlay.view.hidden = NO;
    
    [_activity startAnimating];
    
    [self.moviePlay play];
    
    btn.hidden =YES;
    
    btn.enabled = NO;
    
}




@end
